package com.tang.framework.config.mybatisPlus.interceptor;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;

import com.tang.common.constant.Constant;
import com.tang.common.properties.sys.CoolTeaProperties;
import com.tang.framework.component.TokenService;
import com.tang.framework.core.ContextHold;
import com.tang.framework.core.model.LoginUser;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.schema.Column;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.Objects;

/**
 * @Author tang jun liang
 * @Date 2023/9/3 19:29
 */
@Component
public class CustomizeTenantLineHandler implements TenantLineHandler {

    @Resource
    private TokenService tokenService;

    @Resource
    private CoolTeaProperties coolTeaProperties;


    @Resource
    private RedissonClient redissonClient;


    @Resource
    private HttpServletRequest request;

    @Override
    public String getTenantIdColumn() {
        return "`tenant_id`";
    }

    @Override
    public Expression getTenantId() {
        LoginUser loginUser;
        try {
            if (Objects.nonNull(ContextHold.getRequest())) {
                loginUser = tokenService.getLoginUser(ContextHold.getRequest());
            } else {
                return new StringValue(coolTeaProperties.getDefaultTenantId());
            }
        } catch (Exception e) {
            return new StringValue(coolTeaProperties.getDefaultTenantId());
        }
        if (Objects.isNull(loginUser)) {
            RMap<String, String> tenantMap = redissonClient.getMap(Constant.TENANT_KEY);
            if (Objects.nonNull(ContextHold.getRequest())) {
                if (Objects.isNull(ContextHold.getRequest().getHeader(Constant.DOMAIN))) {
                    return new StringValue(coolTeaProperties.getDefaultTenantId());
                }
                String tenantId = tenantMap.get(request.getHeader(Constant.DOMAIN));
                if (StringUtils.isBlank(tenantId)) {
                    return new StringValue(coolTeaProperties.getDefaultTenantId());
                }
                return new StringValue(tenantId);
            }
        } else {
            return new StringValue(loginUser.getTenantId());
        }
        return new StringValue(coolTeaProperties.getDefaultTenantId());

    }

    // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
    @Override
    public boolean ignoreTable(String tableName) {
        return "TABLES".equals(tableName) || "columns".equals(tableName) ||
                "STATISTICS".equals(tableName) || "sys_menu".equals(tableName) ||
                "sys_dict_type".equals(tableName) ||
                "sys_dict_value".equals(tableName) ||
                "gen_table".equals(tableName) || "gen_field".equals(tableName)
                || "gen_field_type".equals(tableName)
                ;
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return TenantLineHandler.super.ignoreInsert(columns, tenantIdColumn);
    }
}
